package com.ziki.test.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String smtphost = "mail.otaservice.com"; // 发送邮件服务器
        String user = "15905367059@otaservice.com"; // 邮件服务器登录用户名
        String password = "hz@@!!##"; // 邮件服务器登录密码
        String from = "15905367059@otaservice.com"; // 发送人邮件地址
        String to = "15905367059@otaservice.com"; // 接受人邮件地址
        String subject = "Java Mail Subject!"; // 邮件标题
        String body = "Java Mail Body!"; // 邮件内容

        // 以下为发送程序，用户无需改动
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", smtphost);
            props.put("mail.smtp.auth", "true");
            Session ssn = Session.getInstance(props, null);

            MimeMessage message = new MimeMessage(ssn);

            InternetAddress fromAddress = new InternetAddress(from);
            message.setFrom(fromAddress);
            InternetAddress toAddress = new InternetAddress(to);
            message.addRecipient(Message.RecipientType.TO, toAddress);

            message.setSubject(subject);
            message.setText(body);

            Transport transport = ssn.getTransport("smtp");
            transport.connect(smtphost, user, password);
            transport.sendMessage(message, message
                    .getRecipients(Message.RecipientType.TO));
            // transport.send(message);
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
